package cn.stylefeng.guns.modular.packreview.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.packreview.entity.PickingList;
import cn.stylefeng.guns.modular.packreview.model.params.PackReviewParam;
import cn.stylefeng.guns.modular.packreview.model.params.PickingListParam;
import cn.stylefeng.guns.modular.packreview.service.PickingListService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


/**
 * 控制器
 *
 * @author wp
 * @Date 2025-07-26 16:58:53
 */
@Controller
@RequestMapping("/pickingList")
public class PickingListController extends BaseController {

    private String PREFIX = "/pickingList";

    @Autowired
    private PickingListService pickingListService;

    @Autowired
    private UserService userService;
    @Autowired
    private BasicCustomerService basicCustomerService;
    @Autowired
    private BasicWarehouseService basicWarehouseService;

    private Lock lock = new ReentrantLock();
    /**
     * 跳转到主页面
     *
     * @author wp
     * @Date 2025-07-26
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/pickingList.html";
    }

    /**
     * 新增页面
     *
     * @author wp
     * @Date 2025-07-26
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/pickingList_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wp
     * @Date 2025-07-26
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/pickingList_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wp
     * @Date 2025-07-26
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PickingListParam pickingListParam) {
        this.pickingListService.add(pickingListParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author wp
     * @Date 2025-07-26
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(PickingListParam pickingListParam) {
        this.pickingListService.update(pickingListParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wp
     * @Date 2025-07-26
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PickingListParam pickingListParam) {
        this.pickingListService.delete(pickingListParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wp
     * @Date 2025-07-26
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(PickingListParam pickingListParam) {
        PickingList detail = this.pickingListService.getById(pickingListParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wp
     * @Date 2025-07-26
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PickingListParam pickingListParam,String condition ) {
        return this.pickingListService.findPageBySpec(pickingListParam,condition);
    }



    /**
     * 导入页面
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择客户
        model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        //查询可选择仓库
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/pickingList_import.html";
    }

    /**
     * 导入接口
     * @param file
     * @param pickingListParam
     * @return
     */
    @RequestMapping(value = "/importPickingListExcel" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData importPickingListExcel(@RequestPart("file") MultipartFile file, PickingListParam pickingListParam)  {
        ResponseData data = new ResponseData();
        lock.lock();//获取锁
        try {
            data = pickingListService.getExceldata(file,pickingListParam,data);//处理导入数据
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//释放锁
        }
        return data;
    }

    /**
     * 下载模板
     * @param request
     *
     **/
    @RequestMapping("downloadExcelTemplate")
    public void downloadExcelTemplate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = "assets/excel/dbfh_pickingList.xls";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("打包复核管理-拣货单模板" + ".xls").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


